package com.viroyal.monitorlib.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * @author shuwoom
 * @email 294299195@qq.com
 * @date 2015-9-2
 * @update 2015-9-2
 * @des FileUtils
 */
public class FileUtils {

    public static long getStorageSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        long totalSize = totalBlocks * blockSize;

        return totalSize;
    }

    public static long getStorageAvailableSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        long availSize = availableBlocks * blockSize;

        return availSize;
    }

    //删除文件
    public static void delFile(String filepath) {
        File file = new File(filepath);
        if (file.isFile()) {
            try {
                file.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        file.exists();
    }

    //apk获取包名
    public static String getPackageName(String filePath, Context context) {
        String packageName = "";
        PackageManager packageManager = context.getPackageManager();
        PackageInfo info = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            packageName = appInfo.packageName;  //获取安装包名称
        }
        return packageName;
    }

    //apk获取入口Activity
    public static String getLauncherActivityName(String filePath, Context context) {
        String launcherActivityName = "";
        PackageManager packageManager = context.getPackageManager();
        PackageInfo info = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            launcherActivityName = appInfo.name;
        }
        return launcherActivityName;
    }

}
